package fileDemo.mfile;

import org.apache.log4j.Logger;
import org.junit.Test;

import java.io.*;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author Ajie
 * @date 2020-09-24
 * @function
 */
public class FileUtilsTest {
    private static final Logger logger = Logger.getLogger(FileUtilsTest.class);
    private static int sBufferSize = 8192;

    @Test
    public void testIsExist() {
      System.out.println("testIsExist:");
        File file = new File("/Users/macpro/ideaWorkSpace/MyJava/src/fileDemo/mfile/test.tt");

        FileUtils.writeFileFromString(file,"abc");
        System.out.println("FileIOUtilsTest:testIsExist write:"+file.exists());
        boolean b = file.renameTo(new File(file.getParentFile()+File.separator+"abcd.tt"));
        System.out.println("FileIOUtilsTest:testIsExist rename:"+b);
        System.out.println("FileIOUtilsTest:testIsExist:"+file.exists());
    }
    @Test
    public void copyoffset(){
        System.out.println("copyoffset");
        try {
            FileInputStream fileInputStream = new FileInputStream("./src/fileDemo/mfile/test/text");
            fileInputStream.skip(2);
//            FileIOUtils.copyFileUsingFileChannels(new File("./src/fileDemo.mfile/saaszip8.zip"),new File("./src/fileDemo.mfile/test/copyzip8.zip"));
            writeFileFromIS(new File("./src/fileDemo/mfile/test/textcopy")
                    ,fileInputStream
                    ,false);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void readIs2String(){
        try {
            String s = FileUtils.readIs2String(new FileInputStream(new File("/Users/macpro/ideaWorkSpace/MyJava/src/fileDemo/mfile/webmd5.txt")), null);
            System.out.println("FileIOUtilsTest:readIs2String:"+s);
            try {
                String[] s1 = s.split(" ");
                if (s1.length>1){
                    System.out.println("FileIOUtilsTest:readIs2String:md5:"+s1[0]);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void skipFile(){
        try {
            String origin = "./src/fileDemo/mfile/test/text";
            RandomAccessFile randomAccessFile = new RandomAccessFile(origin,"r");
            randomAccessFile.seek(2);
            MappedByteBuffer byteBuffer = randomAccessFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, randomAccessFile.length());
            String path = "./src/fileDemo/mfile/test/textcopy";
            File file = new File(path);
            if (file.exists())file.delete();

            FileOutputStream fileOutputStream = new FileOutputStream(file);
            File originF = new File(origin);
            long length = originF.length();
            System.out.println("origi length:"+length);
//            byteBuffer.position(2).limit(0+(int)length);
            byteBuffer.position(2).limit(3);
            fileOutputStream.getChannel().write(byteBuffer);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static boolean writeFileFromIS(final File file,
                                          final InputStream is,
                                          final boolean append) {
        if (!createOrExistsFile(file) || is == null) return false;
        OutputStream os = null;
        try {
            os = new BufferedOutputStream(new FileOutputStream(file, append));
            byte data[] = new byte[sBufferSize];
            int len;
            boolean isFirst = true;
//            while ((len = is.read(data, :0, sBufferSize)) != -1) {//
            while ((len = is.read(data, 0, sBufferSize)) != -1) {
                System.out.println("while....");
                os.write(data, 0, len);
                isFirst = false;
            }
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (os != null) {
                    os.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static boolean createOrExistsFile(final File file) {
        if (file == null) return false;
        if (file.exists()) return file.isFile();
        if (!createOrExistsDir(file.getParentFile())) return false;
        try {
            return file.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    private static boolean createOrExistsDir(final File file) {
        return file != null && (file.exists() ? file.isDirectory() : file.mkdirs());
    }


    @Test
    public void copyTest(){
        try {
            FileUtils.copyFileUsingFileChannels(new File("./src/fileDemo.mfile/saaszip8.zip"),new File("./src/fileDemo.mfile/test/copyzip8.zip"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


}
